import os
import shutil

# 定义要处理的目录路径
target_dir = '/Volumes/jdssd5/H图片/Pixiv/'

# 遍历目录及其子目录
for root, dirs, files in os.walk(target_dir, topdown=False):
    # 遍历文件夹
    for dir_name in dirs:
        dir_path = os.path.join(root, dir_name)
        try:
            # 删除文件夹
            shutil.rmtree(dir_path)
            print(f"已删除文件夹: {dir_path}")
        except Exception as e:
            print(f"删除文件夹 {dir_path} 时出错: {e}")

    # 遍历文件
    for file_name in files:
        if not file_name.endswith('.pdf'):
            file_path = os.path.join(root, file_name)
            try:
                # 删除非 .pdf 文件
                os.remove(file_path)
                print(f"已删除文件: {file_path}")
            except Exception as e:
                print(f"删除文件 {file_path} 时出错: {e}")